package com.hrbxlh.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hrbxlh.actionImpl.CourseActionImpl;
import com.hrbxlh.actionImpl.DepartMentActionImpl;
import com.hrbxlh.actionImpl.ProfessionActionImpl;
import com.hrbxlh.util.SessionValues;


/**
* -------------------------------------------------------
* Copyright (c) 2017, 鑫联华信息科技股份有限公司
* All rights reserved.
* FileName：DepartMentAction.java
* Description：院系action类
* History：
* Date           Author               Desc
* 2017.3.30		  sy                   建立
* 2017.04.11	  xzx				   添加院系管理部分全部代码		
* -------------------------------------------------------
*/
@Controller
public class DepartMentAction {
	
	@Autowired
	@Qualifier("departMentActionImpl")
	private DepartMentActionImpl departMentActionImpl;
	
	@Autowired
	@Qualifier("professionActionImpl")
	private ProfessionActionImpl professionActionImpl;
	
	@Autowired
	@Qualifier("courseActionImpl")
	private CourseActionImpl courseActionImpl;
	
	/**xzx
	 * @description 跳转到登录人的主页的action
	 * @param request
	 * @param response
	 * @return  跳转到登录人主页
	 * @throws IOException
	 */
	@RequestMapping(value = "/skipMyPage", method = RequestMethod.GET)
	public ModelAndView skipMyPage(String jsonStr, HttpServletRequest request, HttpServletResponse response) throws IOException {		
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放返回值
		Map<String, Object> returnMap = new HashMap<String, Object>();
		//返回字符串
		String reJsonStr = "";
		//存放页面
		String pageName = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(DepartMentAction.class.getName());
		try {
			//获取登录人信息
			SessionValues session = new SessionValues();
			Map<String, Object> sessionValues = session.getSessionValues(new String[]{"roleId"}, request);
			if(sessionValues.get("roleId") != null){
				int roleId = (Integer)sessionValues.get("roleId");
				if(roleId == 1){
					pageName = "redirect:/skipMyCourse"; 
				}else if(roleId == 2){
					pageName = "redirect:/skipMyCourse"; 
				}else if(roleId == 3){
					pageName = "redirect:/skipScheduleMana"; 
				}else if(roleId == 4){
					pageName = "redirect:/skipScheduleMana"; 
				}
			}else{
				pageName = "redirect:/beforeLogin"; 
			}
			//获取我的消息
			List<Map<String, Object>> noticeList  = courseActionImpl.getMyNotice(request);
			resultMap.put("noticeList", noticeList);
		} catch (Exception e) {
			resultMap.put("errMsg", "跳转页面失败");
			e.printStackTrace();
			logger.error("跳转主页面出现异常", e);
			pageName = "public/pageerror";
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			returnMap.put("result", reJsonStr);
			System.out.println("返回的  :" + reJsonStr);
		}
		return new ModelAndView(pageName, returnMap);
	}
	
	/**
	 * @param request
	 * @param response
	 * @return  通过院系id Ajax查询专业集合。
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/getProfessionListByDepmentId", method = RequestMethod.POST)
	public void getProfessionListByDepmentId(String jsonStr, HttpServletRequest request, HttpServletResponse response) throws IOException {		
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//返回字符串
		String reJsonStr = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(ClassAction.class.getName());
		try {
			
			//获取参数值
			Map<String,Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
			//查询某院系下的专业集合
			List<Map<String, Object>> returnList = professionActionImpl.getProfessionList(paramMap);
			//返回值
			resultMap.put("professionList", returnList);
			
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("Ajax查询专业集合", e);
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			System.out.println("返回的："+reJsonStr);
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter write = response.getWriter();
			write.println(reJsonStr);
			write.flush();
		}
	}

	/**xzx
	 * @description 进入院系管理列表  分页
	 * @param request
	 * @param response
	 * //Map<String, Object> mapUse=new HashMap<String, Object>();
	   //mapUse.put("pageNow", 1);
	   //mapUse.put("pageSize", 10);
	   //jsonStr = mapper.writeValueAsString(mapUse);
	 * @return  跳转到院系管理列表页面
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/skipDepartMentMana", method = RequestMethod.GET)
	public ModelAndView skipDepartMentMana(String jsonStr, HttpServletRequest request, HttpServletResponse response) throws IOException {		
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放返回值
		Map<String, Object> returnMap = new HashMap<String, Object>();
		//返回字符串
		String reJsonStr = "";
		//存放页面
		String pageName = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(DepartMentAction.class.getName());
		try {

			Map<String,Object> preParamMap = new HashMap<String, Object>();
			//获取参数值
			if(jsonStr != null){
				preParamMap = mapper.readValue(jsonStr, resultMap.getClass());
			}
			//获取全部院系信息列表
			Map<String, Object> departmentMap = departMentActionImpl.getDepartmentList(preParamMap);
			if(departmentMap != null){
				resultMap.put("departmentList", departmentMap.get("departmentList"));
				resultMap.put("totalPage", departmentMap.get("totalPage"));
				resultMap.put("pageNow", departmentMap.get("pageNow"));
			}
			//获取我的消息
			List<Map<String, Object>> noticeList  = courseActionImpl.getMyNotice(request);
			resultMap.put("noticeList", noticeList);
			//返回条件
			resultMap.put("pageSize", preParamMap.get("pageSize"));
			//院系管理页面名称
			pageName = "pc/departmentMana";
		} catch (Exception e) {
			resultMap.put("errMsg", "跳转页面失败");
			e.printStackTrace();
			logger.error("跳转院系管理页面出现异常", e);
			pageName = "public/pageerror";
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			returnMap.put("result", reJsonStr);
			System.out.println("返回的  :" + reJsonStr);
		}
		return new ModelAndView(pageName, returnMap);
	}
	
	/**xzx
	 * @description 进入添加院系页面
	 * @param request
	 * @param response
	 * @return  跳转到添加院系页面
	 * @throws IOException
	 */
	@RequestMapping(value = "/skipAddDepartMent", method = RequestMethod.GET)
	public ModelAndView skipAddDepartMent(HttpServletRequest request, HttpServletResponse response) throws IOException {		
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放返回值
		Map<String, Object> returnMap = new HashMap<String, Object>();
		//返回字符串
		String reJsonStr = "";
		//存放页面
		String pageName = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(DepartMentAction.class.getName());
		try {
			//获取我的消息
			List<Map<String, Object>> noticeList  = courseActionImpl.getMyNotice(request);
			resultMap.put("noticeList", noticeList);
			//添加院系页面名称
			pageName = "pc/addDepartment";
		} catch (Exception e) {
			resultMap.put("errMsg", "跳转页面失败");
			e.printStackTrace();
			logger.error("跳转添加院系页面出现异常", e);
			pageName = "public/pageerror";
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			returnMap.put("result", reJsonStr);
			System.out.println("返回的  :" + reJsonStr);
		}
		return new ModelAndView(pageName, returnMap);
	}
	
	/**xzx
	 * @dscription 添加院系
	 * @param request
	 * @param response
	 * //Map<String, Object> mapUse=new HashMap<String, Object>();
	   //mapUse.put("departmentName", "哼哼哼");
	   //jsonStr = mapper.writeValueAsString(mapUse);
	 * @return  院系名称
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/addDepartment", method = RequestMethod.POST)
	public void  addDepartment (HttpServletResponse response, HttpServletRequest request, String jsonStr) throws IOException {
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//返回字符串
		String reJsonStr = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(DepartMentAction.class.getName());
		//成功失败标识
		String resultMark = "fail";
		try { 
			//获取参数值
			Map<String,Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
			//添加院系信息
			Map<String, Object> addResultMap = departMentActionImpl.addDepartment(paramMap);
			Boolean flag = (Boolean)addResultMap.get("result");
			//返回参数
			resultMark = (flag) ? "success" : "fail";
			resultMap.put("result", resultMark);
			if(resultMark.equals("fail")){
				resultMap.put("failMsg", addResultMap.get("failMsg"));
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("添加院系出现异常", e);
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			System.out.println("返回的："+reJsonStr);
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter write = response.getWriter();
			write.println(reJsonStr);
			write.flush();
		}
	}
	
	/**xzx
	 * @description 进入修改院系页面
	 * @param request
	 * @param response
		//Map<String, Object> mapUse=new HashMap<String, Object>();
		//mapUse.put("departmentId", 21);
		//jsonStr = mapper.writeValueAsString(mapUse);
	 * @return  跳转到修改院系页面
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/skipSaveDepartMent", method = RequestMethod.GET)
	public ModelAndView skipSaveDepartMent(String jsonStr, HttpServletRequest request, HttpServletResponse response) throws IOException {		
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放返回值
		Map<String, Object> returnMap = new HashMap<String, Object>();
		//返回字符串
		String reJsonStr = "";
		//存放页面
		String pageName = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(DepartMentAction.class.getName());
		try {
			//获取参数值
			Map<String,Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
			resultMap = departMentActionImpl.getDepartmentDetail(paramMap);
			//获取我的消息
			List<Map<String, Object>> noticeList  = courseActionImpl.getMyNotice(request);
			resultMap.put("noticeList", noticeList);
			//修改院系页面名称
			pageName = "pc/saveDepartment";
		} catch (Exception e) {
			resultMap.put("errMsg", "跳转页面失败");
			e.printStackTrace();
			logger.error("跳转修改院系页面出现异常", e);
			pageName = "public/pageerror";
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			returnMap.put("result", reJsonStr);
			System.out.println("返回的  :" + reJsonStr);
		}
		return new ModelAndView(pageName, returnMap);
	}
	
	/**xzx
	 * @dscription 修改院系
	 * @param request
	 * @param response
	 * 	//Map<String, Object> mapUse=new HashMap<String, Object>();
		//mapUse.put("departmentName", "wawawaawaw");
		//mapUse.put("departmentId", 9);
		//jsonStr = mapper.writeValueAsString(mapUse);
	 * @return  院系名称
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/saveDepartment", method = RequestMethod.POST)
	public void  saveDepartment (HttpServletResponse response, HttpServletRequest request, String jsonStr) throws IOException {
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//返回字符串
		String reJsonStr = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(DepartMentAction.class.getName());
		//成功失败标识
		String resultMark = "fail";
		try { 
			//获取参数值
			Map<String,Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
			//修改院系信息
			Map<String, Object> saveResultMap = departMentActionImpl.saveDepartment(paramMap);
			Boolean flag = (Boolean)saveResultMap.get("result");
			//返回参数
			resultMark = (flag) ? "success" : "fail";
			resultMap.put("result", resultMark);
			if(resultMark.equals("fail")){
				resultMap.put("failMsg", saveResultMap.get("failMsg"));
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("修改院系出现异常", e);
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			System.out.println("返回的："+reJsonStr);
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter write = response.getWriter();
			write.println(reJsonStr);
			write.flush();
		}
	}
	
	/**xzx
	 * @dscription 删除院系
	 * @param request
	 * @param response
	 * //Map<String, Object> mapUse=new HashMap<String, Object>();
	   //mapUse.put("departmentId", 10);
	   //jsonStr = mapper.writeValueAsString(mapUse);
	 * @return  院系名称
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/delDepartment", method = RequestMethod.POST)
	public void  delDepartment (HttpServletResponse response, HttpServletRequest request, String jsonStr) throws IOException {
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//返回字符串
		String reJsonStr = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(DepartMentAction.class.getName());
		//成功失败标识
		String resultMark = "fail";
		try { 
			//获取参数值
			Map<String,Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
			//删除院系信息
			Map<String, Object> result = departMentActionImpl.delDepartment(paramMap);
			boolean resultMarkUse = (Boolean)result.get("result");
			//返回参数
			resultMark = (resultMarkUse) ? "success" : "fail";
			resultMap.put("result", resultMark);
			if(!resultMarkUse){
				resultMap.put("failMsg",result.get("failMsg"));
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("删除院系出现异常", e);
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			System.out.println("返回的："+reJsonStr);
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter write = response.getWriter();
			write.println(reJsonStr);
			write.flush();
		}
	}
	
	/**xzx
	 * @description 某个院系的专业管理列表
	 * @param request
	 * @param response
	 * //Map<String, Object> mapUse=new HashMap<String, Object>();
       //mapUse.put("pageNow", 1);
	   //mapUse.put("pageSize", 10);
	   //mapUse.put("departmentId", 8);
	   //jsonStr = mapper.writeValueAsString(mapUse);
	 * @return  跳转到某院系专业管理列表页面
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/skipDepProfessionMana", method = RequestMethod.GET)
	public ModelAndView skipDepProfessionMana(String jsonStr, HttpServletRequest request, HttpServletResponse response) throws IOException {		
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放返回值
		Map<String, Object> returnMap = new HashMap<String, Object>();
		//返回字符串
		String reJsonStr = "";
		//存放页面
		String pageName = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(DepartMentAction.class.getName());
		try {
			Map<String,Object> preParamMap = new HashMap<String, Object>();
			//获取参数值
			if(jsonStr != null){
				preParamMap = mapper.readValue(jsonStr, resultMap.getClass());
			}
			//获取某院系全部专业信息列表
			Map<String, Object> professionMap = departMentActionImpl.getDepartmentProfesList(preParamMap);
			if(professionMap != null){
				resultMap.put("professionList", professionMap.get("professionList"));
				resultMap.put("totalPage", professionMap.get("totalPage"));
				resultMap.put("pageNow", professionMap.get("pageNow"));
			}
			//返回条件
			resultMap.put("pageSize", preParamMap.get("pageSize"));
			resultMap.put("departmentId", preParamMap.get("departmentId"));
			Map<String, Object> departmentMsg = departMentActionImpl.getDepartmentDetail(preParamMap);
			resultMap.put("departmentMsg", departmentMsg);
			//获取我的消息
			List<Map<String, Object>> noticeList  = courseActionImpl.getMyNotice(request);
			resultMap.put("noticeList", noticeList);
			//院系的专业管理页面名称
			pageName = "pc/depProfessMana";
		} catch (Exception e) {
			resultMap.put("errMsg", "跳转页面失败");
			e.printStackTrace();
			logger.error("跳转专业管理页面出现异常", e);
			pageName = "public/pageerror";
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			returnMap.put("result", reJsonStr);
			System.out.println("返回的  :" + reJsonStr);
		}
		return new ModelAndView(pageName, returnMap);
	}
	
}